#### Figure S3: Perceptions of Democratic Norm Violations + Political Violence Support by PID Strength ####

# Libraries
# library(here)
# library(rio)
# library(tidyverse)
# library(ggthemes)
# library(srvyr)
# library(lubridate)

# data_pnas = import(here("Data","data_pnas.rds"))
# source(here("Code","Functions","funcs_theme.R"))

violence_perc = paste0("violence",c(3,6),"_perception")
norm_perc = paste0("norm_",c("judges","polling","censorship","loyalty"),"_perception")

cs_perception_strength = data_pnas |> 
  filter(pid7 %in% c(1:3,5:7)) |>
  mutate(pid_str = case_when(
    pid7 == 1 ~ "Strong Democrat",
    pid7 %in% 2:3 ~ "Weak/Lean Democrat",
    pid7 %in% 5:6 ~ "Weak/Lean Republican",
    pid7 == 7 ~ "Strong Republican"
  )) |>
  select(pid_str, all_of(c(violence_perc, norm_perc)), uid, weight) |> 
  pivot_longer(-c("pid_str","uid","weight"), names_to = "var", values_to = "val") |> 
  mutate(val = val/100) |> 
  as_survey_design(ids = uid, weights = weight) |> 
  group_by(pid_str, var) |> 
  summarise(val_sum = survey_mean(val, na.rm = T, vartype = "ci")) |> 
  ungroup() |> 
  mutate(var_typ = ifelse(str_detect(var,"norm"), "Democratic Norms", "Violence"),
         var = recode_factor(var,
                             norm_polling_perception = "Reduce outparty\npolling stations",
                             norm_loyalty_perception = "More loyal to\nparty than election\nrules and constitution\nin contested elections",
                             norm_judges_perception = "Ignore outparty\ncourt decisions",
                             norm_executive_perception = "President should\ncircumvent congress",
                             norm_censorship_perception = "Censor partisan\nmedia",
                             violence6_perception = "Murder",
                             violence3_perception = "Assault")
  ) |> 
  separate(pid_str, into = c("strength", "party"), sep = " ")

fig_s3 = cs_perception_strength |> 
  ggplot(aes(x = val_sum, y = var,
             group = interaction(party, strength),
             label = paste0(round(val_sum,3)*100,"%"))) +
  geom_pointrange(aes(xmin = val_sum_low,
                      xmax = val_sum_upp,
                      color = party,
                      shape = strength),
                  position = position_dodge(.8)) +
  geom_text(aes(x = val_sum + .13),
            position = position_dodge(.8)) +
  facet_wrap(~ var_typ, scales = "free") +
  scale_color_manual(name = "Party",
                     values = c("blue","red")) +
  scale_shape_manual(name = "PID Strength",
                     values = 16:17) +
  scale_x_continuous(labels = scales::percent,
                     limits = c(0,1)) +
  labs(x = "Percent Perceived Out-Party Support",
       y = NULL,
       caption = "95% Confidence Intervals") +
  theme_prl()

print(fig_s3)

ggsave(here("Plots","Supplementary","figure_s3.png"),
       fig_s3,
       units = "in",
       width = 9, height = 6,
       dpi = 600)